#!/usr/bin/perl
#-------------------------------------------------------------------------------------------
# File:		perl-base.pl
# Author:	A.C. Verbeck
# Date:		October 9, 2013
# Version:	1
# Summary:	base perl script for processing files.
# Description:
#			For years, I have used AWK for this processing.  However, there are many cases 
#			where the limitations prevented things from working as desired.
#
#			The goal is to use perl instead as it offers a better set of tools compared to AWK.
#
#			This is the starting point for these scripts.
#
# Command Line Arguments (with defaults):
#			--input="input.txt"			String:		Input file
#			--output="output.txt":		String:		Output file
#			--module="default":			String:		Default object used for processing
#			--verbose					Flag:		Display useless information
#			--help						Flag:		Display help string
#			--man						Flag:		Display the full man page
#
# $Id$
#-------------------------------------------------------------------------------------------
#	Copyright and Disclaimer Notice
#
#	Copyright (c) 2013-2014 A.C. Verbeck
#
#	This file is part of perl-base: AWK-like processing system
#
#	perl-base is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	perl-base is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with perl-base. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------------------
	use strict;
	use warnings;

	use Cwd;
	use trace;
	use TimeStamp;

	use Pod::Usage;
	use Getopt::Long;
	
	my $i_file	= "input.txt";
	my $o_file	= "output.txt";
	my $p_mod	= "default";
	my ($verbose, $help, $man, $fh);

#-------------------------------------------------------------------------------------------
#	Basic Application variables
#-------------------------------------------------------------------------------------------
	my @fPath = split(/[\\\/]/,$0);												# set the path array
	my $fPathCt = @fPath;														# get the path depth
	my @fName = split(/\./,$fPath[$fPathCt-1]);									# split the filename on the "dots"
	my $fNameCt = @fName;														# get the filename count
	my $ScriptName  = $fName[0];												# set the script root name (assumes only one dot in name e.g. abcd-abc.pl)

#-------------------------------------------------------------------------------------------
#	Argument processing:
#	--input="input.txt":		String:		Input file
#	--output="output.txt":		String:		Output file
#	--module="default":			String:		Default object used for processing
#	--verbose					Flag:		Display useless information
#	--help						Flag:		Display help string
#	--man						Flag:		Display the full man page
#-------------------------------------------------------------------------------------------
	$verbose=0; $help=0; $man=0;												# initialize the local variables
	GetOptions(	"input=s"   =>	\$i_file,										# string
				"output=s"  =>	\$o_file,										# string
				"module=s"  =>	\$p_mod,										# string
				"help"		=>	\$help,											# flag
				"man"		=>	\$man,											# flag
				"verbose"   =>	\$verbose)										# flag
	or die("Error in command line arguments\n");

	if ($help == 1) {															# test for help
		pod2usage(1);															# display help, exit
	}
	if ($man == 1) {															# test for man
		pod2usage(-exitval => 0, -verbose => 2);								# display the full man page, exit
	}
	trace_init($verbose);														# init the trace module with verbose
	trace("Verbose enabled.");													# trace if verbose is enabled

#-------------------------------------------------------------------------------------------
# Get current directory and date / time
#-------------------------------------------------------------------------------------------
	my $CurrentDir = cwd();														# get current working directory
	my $DateStr = timestamp();													# get the timestamp

#-------------------------------------------------------------------------------------------
#	Read / Process file:
#-------------------------------------------------------------------------------------------
	eval "use $p_mod";															# dynamically load the module
	my $op=$p_mod->new();														# create a new processing module

	$op->init($i_file, $o_file);												# init the processing module
	$op->begin();
	open $fh, $i_file;															# open input file
	while (<$fh>) {																# read a lone
		$op->line_proc($_);														# process the line
	}
	close $fh;																	# close input file
	$op->end();
	
#-------------------------------------------------------------------------------------------
#	End of execution
#-------------------------------------------------------------------------------------------
	trace("Exit");
	exit();

__END__

=head1 NAME

perl-base.pl -- Base script for conversion/analysis scripts.

=head1 SYNOPSIS

perl-base.pl [options]

 Options:
   --input     input file (e.g. --input=input.txt)
   --output    output file (e.g. --output=output.txt)
   --module    processing module (e.g. --module=default)
   --verbose   display internal processing messages (flag)
   --help      brief help message, then exit (flag)
   --man       full man page, then exit (flag)

=head1 OPTIONS

=over 8

=item B<--input>

This is the B<input> file for the script.
The default is input.txt.

=item B<--output>

This is the B<output> file for the script.
The default is output.txt.

=item B<--module>

This is the B<module> used for processing.
Currently there are only two modules: default and wc.
B<default> is a base module that can be used as a template for further modules.
B<wc> is a demonstration module that emulates the "wc" command.

=item B<--verbose>

B<verbose> gives a lot of internal processing detail.

=item B<--help>

B<help> displays a brief list of parameters and usage, then exits.

=item B<--man>

B<man> prints the complete, manual page, then exits.

=back

=head1 DESCRIPTION

B<perl-base.pl> is a conversion/analysis script written in perl.
For years, I have used AWK for this kind of processing.
However, there are many cases where the limitations of AWK prevented things from working as desired.
Perl offers a much better tool-set compared to AWK.
To start, modules are loaded at runtime to do specific processing.
Each module is designed to meet specific requirements.

=head1 AUTHOR

A.C. Verbeck

=cut
